package com.example.administrator.xposedtest2;

/**
 * Created by Administrator on 2018/2/9.
 */
import android.view.View;
import android.widget.EditText;

import java.lang.reflect.Field;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
public class Tutorial2 implements IXposedHookLoadPackage {
    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {

        // 将包名不是 com.kali.login 的应用剔除掉
        if (!lpparam.packageName.equals("com.example.administrator.xposedtest2"))
            return;
        XposedBridge.log("Loaded app: " + lpparam.packageName);

        // Hook isOk 方法
        findAndHookMethod("com.example.administrator.xposedtest2.MainActivity", lpparam.classLoader, "onClick", View.class, new XC_MethodHook() {

                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {

                    }

                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        Class clazz = param.thisObject.getClass();
                        XposedBridge.log("class name:"+clazz.getName());
                        // 通过类的字节码得到该类中声明的所有属性，无论私有或公有
                        Field field = clazz.getDeclaredField("etU");
                        // 设置访问权限（这点对于有过android开发经验的可以说很熟悉）
                        field.setAccessible(true);
                        EditText username = (EditText) field.get(param.thisObject);
                        // 设置账号
                        username.setText("admin");
                        XposedBridge.log("uname = " + username.getText().toString());

                    }
                });
    }
}
